
var app = getApp();
var getAppGlobalData = require('../../../templates/copyright/copyright.js');
Page({
  imagePath: '',
  data: {
    id: '',
    template: {},
    globalData: {},
    userImg: '',
    qrImg: '',
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log("onLoad ==>", this);
    var that = this;
    getAppGlobalData.getAppGlobalData(that);
    that.setData({
      globalData: app.globalData
    })
    
    wx.hideShareMenu();
    if (options.id) {
      that.setData({
        id: options.id
      })
    }
    if (options.to_uid) {
      that.setData({
        to_uid: options.to_uid
      })
    }
    that.getShareCard();
  },
  onReady: function () {
    // 页面渲染完成
    var that = this;
  },
  onShow: function () {
    // 页面显示
    var that = this;
  },
  onHide: function () {
    // 页面隐藏 
  },
  onUnload: function () {
    // 页面关闭 
  },
  onPullDownRefresh: function () {
    // 监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底
  },
  onShareAppMessage: function (res) {
    // 用户点击右上角分_享 
  },
  getShareCard: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/card',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        to_uid: wx.getStorageSync("userid"),
        from_id: wx.getStorageSync("userid")
      },
      success: function (res) {
        console.log("entry/wxapp/card ==>", res)
        if (!res.data.errno) {
          that.setData({
            shareData: res.data.data,
          })
          let url = res.data.data.info.avatar;
          that.getImgUrl(url);
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getImgUrl: function (url) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/image',
      'cachetime': '30',
      'method': 'POST',
      'responseType': 'arraybuffer',
      'data': {
        imgUrl: url,
        to_uid: that.data.shareData.to_uid
      },
      success: function (res) {
        console.log("entry/wxapp/image ==>", res)
        if (!res.data.errno) {
          that.setData({
            userImg: res.data.data.image
          })
          that.toGetQR();
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toGetQR: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/releaseQrDetail',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        id: that.data.id
      },
      success: function (res) {
        console.log("获取二维码 entry/wxapp/releaseQrDetail ==>", res)
        if (!res.data.errno) {
          that.setData({
            codeData: res.data.data
          })

          let tmpData = {
            width: '670rpx',
            height: '840rpx',
            background: '#faf8f5',
            borderRadius: '15rpx',
            views: [
              {
                type: 'rect',
                css: {
                  top: '0rpx',
                  left: '0rpx',
                  color: 'white',
                  borderRadius: '15rpx',
                  borderWidth: '0rpx',
                  width: '668rpx',
                  height: '838rpx',
                },
              },
              {
                type: 'rect',
                css: {
                  top: '0rpx',
                  left: '0rpx',
                  color: '#faf8f5',
                  borderRadius: 0,
                  borderWidth: 0,
                  width: '668rpx',
                  height: '293rpx',
                },
              },
              {
                type: 'image',
                url: that.data.userImg,
                css: {
                  top: '44rpx',
                  left: '22rpx',
                  width: '92rpx',
                  height: '92rpx',
                  rotate: 0,
                  borderRadius: '92rpx',
                },
              },
              {
                type: 'text',
                text: that.data.globalData.company.name,
                css: {
                  fontSize: '30rpx',
                  top: '65rpx',
                  left: '130rpx',
                  color: '#2b2b2b',
                  textDecoration: 'none',
                  align: 'left',
                  width: '540rpx'
                },
              },
              {
                type: 'text',
                text: that.data.codeData.content,
                css: {
                  fontSize: '28rpx',
                  top: '155rpx',
                  left: '26rpx',
                  color: '#333333',
                  textDecoration: 'none',
                  align: 'left',
                  width: '610rpx',
                },
              },
              {
                type: 'image',
                url: that.data.codeData.path,
                css: {
                  top: '363rpx',
                  left: '142rpx',
                  width: '396rpx',
                  height: '396rpx',
                  rotate: 0,
                  borderRadius: 0,
                },
              }
            ]
          }
          that.setData({
            template: tmpData
          })
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  onImgOK: function (e) {
    var that = this;
    that.setData({
      imagePath: e.detail.path
    })
    console.log(e);
  },
  toShareRecord: function () {
    var that = this;
    var recordUrl = app.siteInfo.siteroot + '?i=' + app.siteInfo.uniacid + '&t=' + app.siteInfo.multiid + '&v=' + app.siteInfo.version + '&from=wxapp&' + 'c=entry&a=wxapp&do=record&m=' + app.siteInfo.name;
    // console.log('recordUrl', recordUrl)
    wx.request({
      'url': recordUrl,
      'data': {
        to_uid: app.globalData.to_uid
      },
      'method': 'POST',
      'header': {
        'content-type': 'application/x-www-form-urlencoded'
      },
      'success': function (res) {
        wx.hideNavigationBarLoading();
        wx.hideLoading();
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  saveImage() {
    var that = this;
    console.log("点击保存海报")
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success: function (res) {
        console.log("保存名片成功 ==>", res)
        that.toShareRecord();
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    });
  }
})